package com.dave.serviceribbon.controller

import com.dave.serviceribbon.service.HelloService
import com.sun.tools.corba.se.idl.constExpr.Plus
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.io.File

/**
 * Created by mac on 2018/5/17
 * Describes 此处用@Controller会报404
 */
@Suppress("UNREACHABLE_CODE")
@RestController
class HelloController {
    @Autowired
    internal val helloService: HelloService? = null

    @RequestMapping(value = ["/hi"])
    fun hi(@RequestParam name: String): String {
        //return helloService!!.hiService(name)
        return """
                    艹Tell me and I forget.
                    艹Teach me and I remember.
                    艹Involve me and I learn.
                    艹(Benjamin Franklin) """.trimMargin("艹");
    }

    fun add(a: Int, b: Int): Int? {
        if (1 == a) {
            return a * b
        }
        return null
    }

    fun autoTypeCover(obj: Any): Int? {
        //检测分支
        if (obj is String) {
            //自动转回为String类型
            return obj.length
            val person = Person("dave", "男", 18)

        }
        return null
    }

    //创建一个pojo
    data class Person(val name: String, val gender: String, val age: Int)


    fun lambda() {

        //完整函数类型左 和实例右
        val intPlus: (Int, Int) -> Int = Int::plus
        //标准函数类型和实例写法  会自动推断函数类型
        val sum={x:Int,y:Int->x+y}
        print(sum(1, 3))

        val items= listOf<String>("a","b","c","d")
        items.fold("gg"){acc,e->acc+e}

        items.filter {it.isNotEmpty() }

        //可以使用接收这类型的方法
        val sumFun: Int.(String,String) -> Int={x,y->x.length.plus(y.length)}

        //print(sumFun(13,3))
        fuckMe("xiaomei"){it.toUpperCase()}

    }

    fun fuckMe(leader:String,it:(String)->String):String{

        return "fuck"+it(leader)
        var gg:Int.(String)->Int={a->1.plus(a.length)}
    }

    fun readFile(){

        val file=File("");
        file.readText(Charsets.UTF_8);
    }

}


